#ifndef SAVE_MANAGER_HPP
#	define SAVE_MANAGER_HPP

#	include <cstdio>
#	include "Chess.hpp"
#	include "Action.hpp"
#	include "MoveAction.hpp"
#	include "PawnPromotionAction.hpp"
#	include "CastlingAction.hpp"
#	include "WarnException.hpp"
#	include <cstring>

struct LoadActions
{
	map<unsigned int, list<Action*> > p1_actions;
	map<unsigned int, list<Action*> > p2_actions;
};

class SaveManager
{
	public:
		static SaveManager* getInstance();
		void save(	string filename, 
					const map<unsigned int, list<Action*> >& p1_actions,
					const map<unsigned int, list<Action*> >& p2_actions);
		void load(string filename, LoadActions* load_actions);
		
	private:
		static SaveManager* instance;
		static const char 	header[];
		static char 		curbyte_str[80];
		
		SaveManager();
		~SaveManager();
		
		void writeActions(	const map<unsigned int, list<Action*> >& p1_actions, 
							const map<unsigned int, list<Action*> >& p2_actions);
		void writeAction(Action* action);
		void writeTurnNumber(unsigned int nb_turns);
		void writeColor(Chess::Color color);
		void writePosition(const Position& pos);
		void writeMoveAction(MoveAction* maction);
		void writePromotionType(Piece::Type type);
		void writePawnPromotionAction(PawnPromotionAction* ppaction);
		void writeCastlingType(CastlingAction::Type type);
		void writeCastlingAction(CastlingAction* caction);
		
		void			parseLoadBuffer(LoadActions* load_actions);
		unsigned int 	readTurnNumber();
		Chess::Color 	readColor();
		Position		readPosition();
		MoveAction*		readMoveAction();
		Piece::Type		readPromotionType();
		PawnPromotionAction*	readPawnPromotionAction();
		CastlingAction::Type	readCastlingType();
		CastlingAction*			readCastlingAction();
		
		list<unsigned char> save_buffer;
		list<unsigned char> load_buffer;
		unsigned int		load_curbyte;
};


#endif
